package window;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.PatrickXmlReader;

/**
 * The login window of xls2DB
 * 
 * @author Patrick
 * @date 2010.3.1
 */
public class Login extends JFrame {
    // window attributes
    JLabel type, server, database, username, password;

    JComboBox typeSelect;

    JTextField serverUrl, dbName, user, pwd;

    JButton connect, cancel;
    
    Map<String,String> dbMap;

    public Login(String s) {
        super(s);
        setBounds(200, 200, 300, 300);
        setLayout(null);
        // add panel
        JPanel input = new JPanel();
        JPanel oprator = new JPanel();
        add(input);
        input.setBounds(5, 5, 280, 200);
        input.setLayout(new GridLayout(5, 2, -20, 12));
        add(oprator);
        oprator.setBounds(0, 200, 300, 100);
        oprator.setLayout(null);

        // add databaseSelector
        input.add(type = new JLabel("数据库类型"));
        String[] typeList = PatrickXmlReader.getTypeNames();
        typeSelect = new JComboBox(typeList);
        typeSelect.addActionListener(new TypeListener());
        input.add(typeSelect);
        // before typeselect action fired ,defaultly read the first database info
        try{
            dbMap=PatrickXmlReader.getDetail(typeSelect.getSelectedItem().toString());
            System.out.println("selected database information reading complete,connecting...");
        }catch(Exception e){
            e.printStackTrace();
        }
        // add URL Input
        input.add(server = new JLabel("服务器"));
        input.add(serverUrl = new JTextField(12));
        serverUrl.setText(dbMap.get("connectURL"));
        // add database chooser
        input.add(database = new JLabel("数据库"));
        input.add(dbName = new JTextField(12));
        // add userName input;
        input.add(username = new JLabel("用户名"));
        input.add(user = new JTextField(10));
        // add password
        input.add(password = new JLabel("密码"));
        input.add(pwd = new JTextField(10));

        // oprator
        connect = new JButton("连接");
        connect.addActionListener(new ConnectListener());
        oprator.add(connect);
        connect.setBounds(40, 20, 80, 30);
        cancel = new JButton("取消");
        cancel.addActionListener(new CancelListener());
        oprator.add(cancel);
        cancel.setBounds(160, 20, 80, 30);

        setResizable(false);
        setVisible(true);
        validate();
    }

    // the listener for database choosing
    class TypeListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try{
                dbMap=PatrickXmlReader.getDetail(typeSelect.getSelectedItem().toString());
                System.out.println("selected database information reading complete,connecting...");
            }catch(Exception ee){
                ee.printStackTrace();
            }
            serverUrl.setText(dbMap.get("connectURL"));
            dbName.setText(null);
            user.setText(null);
            pwd.setText(null);
        }
    }

    // the listener for connecting database,in fact we only get the connect information
    class ConnectListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            dbMap.put("serverUrl", serverUrl.getText());
            dbMap.put("database", dbName.getText());
            dbMap.put("user", user.getText());
            dbMap.put("pwd", pwd.getText());
            try {
                new MainFrame(dbMap, "xls2DB-V1.0");
            } catch (Exception ee) {
                ee.printStackTrace();
            }
            setVisible(false);
        }
    }

    // the listener for exiting programme
    class CancelListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}
